<?php
//首页显示
namespace newMobile\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
		$isweixin=is_weixin();
    	if($isweixin){
    		$appid=C('weixin.APP_KEY');
			$appsecret=C('weixin.APP_SECRET');
			$weObj = new \Org\Util\Jssdk($appid,$appsecret);
			$wechat=$weObj->GetSignPackage();
			$this->assign('signPackage',$wechat);
    	}
        session('mode','wechat');
    	$shop=M('shop');
    	$type=I('get.type');
    	if($type!=null){
    		session('index_type',$type);
    	}else{
    		$type=session('index_type');
    	}
    	//综合
    	if($type==0){
    		$order="id desc";
    	}
    	//人气
		if($type==1){
    		$order="click_num desc";
    	}
    	//上架时间
    	if($type==2){
    		$order="addtime desc";
    	}
    	//销量
    	if($type==3){
    		$order="sale_num desc";
    	}
    	//价格
    	if($type==4){
    		$order="price desc";
    	}
    	//拼团
    	$nowtime=time();
    	$ptuan=$shop->where("ishidden=0 and tuanselect=1 and tuanz_starttime<$nowtime and tuanz_endtime>$nowtime")->limit('6')->order('tuan_order desc')->select();
    	$this->assign('ptuan',$ptuan);
    	//商品推荐
    	$recommend=$shop->where('isindex=1 and ishidden=0')->limit('9')->order($order)->select();
    	$this->assign('recommend',$recommend);
    	//猜你喜欢
    	
		$num=10;
		$max=$shop->max('id');
		$min=$shop->min('id');
		
		$i=1;
		$flag=0;
		while($i<=$num){
			$rundnum=rand($min, $max);
			$flag_2=true;
			if($flag!=$rundnum){
				if($like_list){
					foreach($like_list as $key=>$value){
						if($rundnum==$value['id']){
							$flag_2=false;
						}
					}
				}
				if($flag_2){
					$rs=$shop->where('ishidden=0')->getById($rundnum);
					if(!empty($rs)){
						$like_list[]=$rs;
						$i++;
						$flag=$rundnum;
					}
				}
				
				
			}
			
		}
		
    	$this->assign('like_list',$like_list);
    	
    	
    	$hot_list=$shop->where('ishidden=0')->limit('9')->order('sale_num desc')->select();
    	$this->assign('hot_list',$hot_list);
    	
		$this->display();
	}
	//二维码生成
	public function qrcode(){
        Vendor('phpqrcode.phpqrcode');
        //生成二维码图片
        $object = new \QRcode();
        $url=I('get.url');//网址或者是文本内容
        $level=3;
        $size=4;
        $errorCorrectionLevel =intval($level) ;//容错级别
        $matrixPointSize = intval($size);//生成图片大小
        $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
    }
    function subsite(){
    	if($_GET['type']==2){
			session('no_qiecity',1);
            $this->redirect('/');
		}

    	if($_GET['id']){
    		$id=I('get.id');
    		$sys_city=M('sys_city');
    		$rs=$sys_city->find($id);
    		session('site_city',$rs['city_name']);
			session('site_city_id',$id);
			cookie('site_city',$rs['city_name'],604800);
			cookie('site_city_id',$id,604800);
			$this->redirect('/');
    	}
    	$this->display();
    }
    
    public function tsuccess(){
    	$this->success('操作完成','/Article/index',30000);
    }
	
	public function weixinbd(){
		if(session('user_id')){
			$user['wx_openid']=I('post.openid');
			$user['id']=session('user_id');
			$rs=M('users')->save($user);
			if($rs){
				$msg['code']=1;
				$msg['msg']='绑定成功';
			}else{
				$msg['code']=0;
				$msg['msg']='绑定失败';
			}
		}else{
			$msg['code']=0;
			$msg['msg']='未登陆';
		}
		
		$this->ajaxReturn($msg);
	}
	
	public function s_wuliu(){
		
		//send_wuliu('oNkdFxDPDLLzExz0sEHgECV0aPb8','http://m.58daiyan.com','11111','222222222','3333333333333');
	}
	
	public function news(){
        $order=I('get.order',1);
        $stype_id=I('get.type',0);
        $this->assign('order',$order);
        $this->assign('stype_id',$stype_id);

        $rs=M('shop')
			->field('yt_shop.type_id')
			->join('yt_shop_content on yt_shop_content.shop_id=yt_shop.id','left')
			->where("yt_shop_content.story_title <>'' and yt_shop_content.story_content <> ''")
			->select();
        $types=array();
        foreach ( $rs as $k=>$v){
        	$type_id=explode(',',$v['type_id']);
        	if(!in_array($type_id,$types)){
                $types[]=$type_id[1];
			}

		}
        $types_string=implode(',',$types);
        $map['id']=array('in',$types_string);

        $rs=M('ticket_type')->where($map)->select();

        $this->assign('rs',$rs);
		$this->display();
	}
	
	public function ajax_news(){
		$page=I('get.page');
        $order=I('get.order');
        $stype_id=I('get.stype_id');
        if($order==1){
        	$order='yt_shop_content.id desc';
		}
        if($order==2){
            $order='yt_shop_content.story_hits desc';
        }

        if($stype_id!=0){
        	$where_string=" and FIND_IN_SET('".$stype_id."',yt_shop.type_id)";
		}else{
            $where_string='';
		}
		$num=2;
		$strat=($page-1)*$num;
		$html='';
		$count=M('shop_content')
            ->join('yt_shop on yt_shop_content.shop_id=yt_shop.id','left')
			->where('yt_shop_content.story_title is not null and yt_shop_content.story_title!="" and yt_shop_content.story_img is not null and yt_shop_content.story_img!="" '.$where_string)
			->count();
		$page=ceil($count/$num);
		$news=M('shop_content')
			->field('story_title,story_time,story_img,story_hits,shop_id')
            ->join('yt_shop on yt_shop_content.shop_id=yt_shop.id','left')
            ->where('yt_shop_content.story_title is not null and yt_shop_content.story_title!="" and yt_shop_content.story_img is not null and yt_shop_content.story_img!="" '.$where_string)
			->limit($strat,$num)
			->order($order)
			->select();

		foreach($news as $key=>$value){
			$html=$html.'<div class="article-item">
						<a href="'.U('shop/store',array('id'=>$value['shop_id'])).'">
							<div class="article-content">
								<img src="'.$value['story_img'].'">
								<div class="article-title ">
									<div class="article-title-footer">
										<div class="article-footer-lf">
										   <div class="article-icons like-btn" data-refer-id="92">
						            <i class="icon-article-unlike " style="margin-bottom:.1rem"></i>
						            <span class="liken-num">'.date('Y-m-d',$value['story_time']).'</span>
					            </div>
										</div>
										<div class="article-footer-rg">
											<div class="article-icons">
												<i class="icon-read"></i>
												<span class="read-num">'.$value['story_hits'].'</span>
											</div>
										</div>
									</div>
								</div>
								
							</div>
							<div class="article-footer">
								<p>'.$value['story_title'].'</p>
							</div>
						</a>
				  </div>';
		}
		$data['content']=$html;
		$data['pages']=$page;
		$this->ajaxReturn($data);

					
				
		
	}
    

  function get_openid(){
        $appid=C('weixin.APP_KEY');
        $appsecret=C('weixin.APP_SECRET');

        $data = json_decode(file_get_contents("access_token.json"),true);//建立一个文件把第一次请求到的token对象放进去

        if (empty($data) || $data['expire_time']< time()) {
          $json_token=http_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret);
          $access_token=json_decode($json_token,true);
          //获得access_token
          $token=$access_token[access_token];
          if($token) {
            $data['expire_time'] = time()+($access_token['expires_in'] - 100);
            $data['access_token'] = $token;
            $fp = fopen("access_token.json", "w");
            fwrite($fp, json_encode($data));
            fclose($fp);
          }
        }else{
          $token =$data['access_token'];
        }
        $url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=' . $token;
        $rs=get_url_contents($url);
        $rs=json_decode($rs, true);
        $users=array();
    	foreach($rs['data']['openid'] as $key=>$value){
           $get_info_url='https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$token.'&openid='.$value.'&lang=zh_CN ';
           $info=get_url_contents($get_info_url);
           $info=json_decode($info, true);
           $users[]=$info;
           $nickname=$info['nickname'];
           $user_info=M('users')->where("wx_name='$nickname'")->find();
           if($user_info['wx_h5openid']==null){
          	    $data['id']=$user_info['id'];
            	$data['wx_h5openid']=$value;
            	M('users')->save($data);
                echo $user_info['wx_name']."修改成功";
          }
        }
   
    
    }
    public  function  join(){
        $isweixin=is_weixin();
        if($isweixin){
            $appid=C('weixin.APP_KEY');
            $appsecret=C('weixin.APP_SECRET');
            $weObj = new \Org\Util\Jssdk($appid,$appsecret);
            $wechat=$weObj->GetSignPackage();
            $this->assign('signPackage',$wechat);
        }
        $this->display();
    }
    public  function  add_join(){
        $data=I('post.');

        $phone=$data['phone'];
        $rs_phone=M('store_join')->where("phone='$phone' and status=0")->find();
        if($rs_phone){
            $this->ajaxReturn(2);
        }
        $data['type']='pc';
        $data['addtime']=time();

        $rs=M('store_join')->add($data);
        if($rs){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }

    }

    public  function update(){

        $appid = I('get.appid');
        $version = I('get.version');//客户端版本号
        $rsp = array('status' => 0);//默认返回值，不需要升级
        if (isset($appid) && isset($version)) {
            if($appid=="__W2A__m.58daiyan.com"){//校验appid
                //这里是示例代码，真实业务上，最新版本号及relase notes可以存储在数据库或文件中
				$rs=get_web_invoice();
             
                if($version !== $rs['web_appnum']){
                    $rsp['status'] = 1;
                    $rsp['title'] = '应用更新';
                    $rsp['note'] =htmlspecialchars_decode($rs['web_appinfo']);//release notes，支持换行
                    $rsp['url'] = $rs['web_appurl'];//应用升级包下载地址
                }

            }
        }
        exit(json_encode($rsp));
    }
	
	function tj_user(){
	   	$id=I('get.id');
	   	session('tj_userid',$id);
	   	if(session('user_id')){
	   		$this->redirect('/');
	   	}else{
	   		$this->redirect('user/register');
	   	}
	   
	}

    
}
?>
